package com.yehuishou.service.impl;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.yehuishou.dao.BackItemDao;
import com.yehuishou.dao.FileDao;
import com.yehuishou.pojo.BackItem;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.service.BackItemService;


@Service
public class BackItemServiceImpl implements BackItemService {

	
	@Resource
	private FileDao fileDao;
	
	@Resource
	private BackItemDao backItemDao;
	
	@Override
	public List<BackItem> findBypid(Integer pid) {
		return backItemDao.findBypid(pid);
	}

	@Override
	public BackItem getById(Integer id) {
		BackItem ba =backItemDao.getBackItemById(id);
		
		 String img_url =ba.getImg_url();
		 String	fileid="";
			if(img_url!=null&&img_url!="") {
				if(img_url.split(";").length>1){
					
				fileid =img_url.split(";")[1];
					System.out.println(fileid);
				}
				if(fileid!=null&&fileid!=""){
			ImageFile img=fileDao.getOneImage(Integer.parseInt(fileid));
			System.out.println(img);
			
			ba.setImg_url(img.getFilePath());
				
				}
			}
		
		return ba;
	}

	@Override
	public List<BackItem> getBackItems(Integer pid,Integer cid) {
		BackItem back = new BackItem();
		back.setPid(pid);
		back.setCid(cid);
	List<BackItem>	 backlist =backItemDao.getBackItems(back);
	
	for (BackItem backItem : backlist) {
		 String fileid ="";
		 String img_url =backItem.getImg_url();
		System.out.println(img_url!=null&&img_url!="");
		 if(img_url!=null&&img_url!="") {
			if(img_url.split(";").length>1){
				
				fileid =img_url.split(";")[1];
				System.out.println(fileid);
			}
			if(fileid!=null&&fileid!=""){
		ImageFile img=fileDao.getOneImage(Integer.parseInt(fileid));
		System.out.println(img);
		
		backItem.setImg_url(img.getFilePath());
		}
	   }
	}
		
		return backlist;
	}

	@Override
	public List<BackItem> findBycid(Integer cid) {
		List<BackItem> item = backItemDao.findBycid(cid);
		for (BackItem backItem : item) {
			 String fileid ="";
			 String img_url =backItem.getImg_url();
			 if(img_url!=null&&img_url!="") {
				if(img_url.split(";").length>1){
					
					fileid =img_url.split(";")[1];
					System.out.println(fileid);
				}
				if(fileid!=null&&fileid!=""){
			ImageFile img=fileDao.getOneImage(Integer.parseInt(fileid));
			
			backItem.setImg_url(img.getFilePath());
			}
		   }
		}
		return item;
	}

	@Override
	public List<BackItem> getBackItem(String brandname, Integer pid, Integer cid) {
		
		BackItem back = new BackItem();
		back.setPid(pid);
		back.setCid(cid);
		back.setBrandname(brandname);
	List<BackItem>	 backlist =backItemDao.getBackItems(back);
	
	for (BackItem backItem : backlist) {
		 String fileid ="";
		 String img_url =backItem.getImg_url();
		System.out.println(img_url!=null&&img_url!="");
		 if(img_url!=null&&img_url!="") {
			if(img_url.split(";").length>1){
				
				fileid =img_url.split(";")[1];
				System.out.println(fileid);
			}
			if(fileid!=null&&fileid!=""){
		ImageFile img=fileDao.getOneImage(Integer.parseInt(fileid));
		if(img!=null) {
			backItem.setImg_url(img.getFilePath());	
		}
		
		}
	   }
	}
		
		return backlist;
	}

}
